<template>
  <el-container class="admin-container">
    <!-- 侧边栏 -->
    <el-aside width="200px" class="admin-aside">
      <div class="logo">
        <el-icon><Setting /></el-icon>
        <span>管理后台</span>
      </div>
      <el-menu
        :default-active="$route.path"
        router
        background-color="#303133"
        text-color="#fff"
        active-text-color="#ffd04b">
        <el-menu-item index="/admin/idle">
          <el-icon><Goods /></el-icon>
          <span>闲置管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/order">
          <el-icon><Document /></el-icon>
          <span>订单管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/user">
            <el-icon><UserFilled /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container>
      <!-- 顶部栏 -->
      <el-header class="admin-header">
        <span class="header-title">{{ pageTitle }}</span>
        <div class="header-right">
          <span class="admin-name">{{ adminName }}</span>
          <el-button type="danger" size="small" @click="handleLogout">退出登录</el-button>
        </div>
      </el-header>

      <!-- 内容区 -->
      <el-main class="admin-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, Lock, Expand, Fold, Goods, Document, UserFilled, Setting } from '@element-plus/icons-vue'
import api from '@/api'

const router = useRouter()
const route = useRoute()

const adminName = ref('')

const pageTitle = computed(() => {
    const path = route.path
    if (path.includes('/admin/idle')) return '闲置管理'
    if (path.includes('/admin/order')) return '订单管理'
    if (path.includes('/admin/user')) return '用户管理'
    return '管理后台'
  })

onMounted(() => {
  const adminInfo = sessionStorage.getItem('adminInfo')
  if (adminInfo) {
    const info = JSON.parse(adminInfo)
    adminName.value = info.adminName || '管理员'
  }
})

const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    api.loginOut().then(() => {
      sessionStorage.removeItem('adminInfo')
        ElMessage.success('已退出登录')
        router.push('/login-admin')
    })
  }).catch(() => {})
}
</script>

<style scoped>
.admin-container {
  height: 100vh;
}

.admin-aside {
  background-color: #303133;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  gap: 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.admin-header {
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #e9d5ff;
  box-shadow: 0 2px 8px rgba(139, 92, 246, 0.1);
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #8B5CF6;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.admin-name {
  color: #606266;
  font-size: 14px;
}

.admin-main {
  background: #f0eff7;
  padding: 20px;
}
</style>
